<?php
namespace App\Http\Controllers\Admin;

use App\Models\System\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;

/**
 * 用户控制器
 * @package App\Http\Controllers\Api
 */
class AccountController extends BaseController
{
    /**
     * 登陆请求
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required',
        ]);
        try{
            $username = $request->username ?? '';
            $email = $request->email ?? '';
            $password = $request->password ?? '';
            $user = User::query()->where('email', $email)->first();
            if (empty($user)){
                throw new \Exception('账号或密码错误');
            }
            //密码base64解密
            $password = base64_decode($password);
            if (!Hash::check($password, $user->password)){
                throw new \Exception('账号或密码错误');
            }
            $token = Str::random(60);
            //存缓存
            Cache::put("api_token:" . $token, $user->toArray());
            //刷新权限缓存
            if ($user){
                $user->getPermissionList(true);
            }
            return $this->success('登录成功', ['token' => $token]);
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
    }
    /**
     * 获取用户数据
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function getInfo(Request $request)
    {
        try{
            $user = $request->user();
            $user->load('roles', 'roles.permissions');
            list($userRoleList, $userPermissionList) = (new \App\Services\System\User())->getAdminUserConf($user->id);
            //权限列表
            $menuSer = new \App\Services\System\Menu();
            //当前菜单列表
            // $treeMenuList = $menuSer->getHasPermTreeMenuList('admin', $userPermissionList);
            return $this->success('获取成功', ['user' => $user,'roleList' => $userRoleList, 'permList' => $userPermissionList, /*'menuList' => $treeMenuList*/]);
        }catch (\Exception $e){
            return $this->error([], $e->getMessage());
        }
    }

    /**
     * 获取导航
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getConfig(Request $request)
    {
        try{
            $user = $request->user();
            list($userRoleList, $userPermissionList) = (new \App\Services\System\User())->getAdminUserConf($user->id);
            //权限列表
            $menuSer = new \App\Services\System\Menu();
            //当前菜单列表
            $treeMenuList = $menuSer->getHasPermTreeMenuList('admin', $userPermissionList);
            return $this->success('获取成功', [/*'user' => $user,'roleList' => $userRoleList, 'permList' => $userPermissionList, */'menuData' => $treeMenuList]);
        }catch (\Exception $e){
            return $this->error([], $e->getMessage());
        }
    }
    /**
     * 登出
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout(Request $request){
        try{
            $token = $request->bearerToken();
            Cache::forget("api_token:".$token);
            return $this->success('注销成功');
        }catch (\Exception $e){
            return $this->error([], $e->getMessage());
        }
    }

    /**
     * 保存用户信息设置
     */
    public function update(Request $request)
    {
        try{
            $user = $request->user();
            $saveUserData = $request->toArray();
            $saveUserData['id'] = $user->id;
            if(!(new \App\Services\System\User())->baseSetting($saveUserData)){
                throw new \Exception('保存失败');
            }
            return $this->success('保存成功');
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
    }

    /**
     * 重置密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resetPwd(Request $request){
        try{
            $user = $request->user();
            $saveUserData = $request->toArray();
            $saveUserData['id'] = $user->id;
            if(!(new \App\Services\System\User())->resetPwd($saveUserData)){
                throw new \Exception('保存失败');
            }
            return $this->success('保存成功');
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
    }
}
